const fs = require('fs');
const path = require('path');

// 读取淘宝进货单数据
const taobaoDataPath = path.join(__dirname, '通过插件拦截响应数据（淘宝进货单）.md');
let taobaoData;

try {
  const data = fs.readFileSync(taobaoDataPath, 'utf8');
  taobaoData = JSON.parse(data);
  console.log('成功读取淘宝进货单数据');
} catch (error) {
  console.error('读取淘宝进货单数据失败:', error);
  process.exit(1);
}

// 获取第一个数据项
const firstItem = taobaoData.data[0];
const responseData = firstItem.response.data.data;

// 查找所有订单容器
const orderContainers = [];
for (const key in responseData) {
  if (key.startsWith('shopOrderContainer_')) {
    orderContainers.push(key);
  }
}

console.log('找到的订单容器:', orderContainers);

// 检查第一个订单的详细信息
if (orderContainers.length > 0) {
  const firstOrderKey = orderContainers[0];
  const orderId = firstOrderKey.split('_')[1];
  
  console.log('\n第一个订单ID:', orderId);
  
  // 检查订单信息
  const shopInfo = responseData[`shopInfo_${orderId}`];
  const orderPayment = responseData[`orderPayment_${orderId}`];
  
  if (shopInfo) {
    console.log('店铺信息存在');
    console.log('创建时间:', shopInfo.fields.createTime);
    console.log('店铺名称:', shopInfo.fields.shopName);
  } else {
    console.log('店铺信息不存在');
  }
  
  if (orderPayment) {
    console.log('订单支付信息存在');
    console.log('实付款:', orderPayment.fields.actualFee.value);
  } else {
    console.log('订单支付信息不存在');
  }
  
  // 检查订单项
  console.log('\n检查订单项:');
  for (let i = 1; i <= 10; i++) {
    const itemKey1 = `orderItemInfo_${orderId}_${orderId}${i}`;
    const itemKey2 = `orderItemInfo_${orderId}_${orderId}_${i}`;
    
    if (responseData[itemKey1]) {
      console.log(`找到订单项 (模式1): ${itemKey1}`);
      const itemData = responseData[itemKey1].fields.item;
      console.log('  商品标题:', itemData.title);
      console.log('  数量:', itemData.quantity);
      console.log('  价格:', itemData.priceInfo.actualTotalFee);
    }
    
    if (responseData[itemKey2]) {
      console.log(`找到订单项 (模式2): ${itemKey2}`);
      const itemData = responseData[itemKey2].fields.item;
      console.log('  商品标题:', itemData.title);
      console.log('  数量:', itemData.quantity);
      console.log('  价格:', itemData.priceInfo.actualTotalFee);
    }
  }
  
  // 列出所有以orderItemInfo开头的键
  console.log('\n所有订单项相关键:');
  for (const key in responseData) {
    if (key.startsWith('orderItemInfo')) {
      console.log('  ', key);
    }
  }
}